<template>
  <div class="forum-container">
    <m-header title="论坛"></m-header>
    <div class="body-container">
      <scroller :on-refresh="refresh" :on-infinite="infinite">
        <div class="post-list">
          <div
            class="post"
            v-for="(item, index) in posts"
            :key="index"
            @click="$router.push({ path: `/forum/detail/${item.article_id}` })"
          >
            <div class="left">
              <div class="title">{{item.title}}</div>
              <!-- <div class="figures" v-if="item.figures&&item.figures.length>0">
              <img class="figure" v-for="(figure, i) in (item.figures || [])" :key="i" :src="figure"/>
              </div>-->
              <div class="user-info">
                <img class="avatar" :src="item.head_img">
                <div class="name">{{item.author}}</div>
                <div class="view">阅读{{item.read_num}}</div>
                <div class="time">{{item.create_time|getDateDiff}}</div>
              </div>
            </div>
            <div class="right">
              <img class="figure" v-lazy="item.cover_url" alt="">
            </div>
          </div>
        </div>
      </scroller>
      <div class="no-datas" v-if="!loading && !posts.length">
        <img src="../../../assets/imgs/icon-noattention@2x.png" alt>
        <div class="desc">暂无数据</div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapActions } from 'vuex';

export default {
  data () {
    return {
      pageNum: 1,
      isPull: false, // 下拉
      loading: true,
      posts: []
    };
  },
  // mounted () {
  //   this.getPostList({page: 1}).then(res => {
  //     if (res) {
  //       this.posts = res.items;
  //     }
  //   });
  // },
  methods: {
    ...mapActions({
      getPostList: 'forum/getPostList'
    }),
    refresh (done) {
      // 下拉
      this.isPull = true;
      this.pageNum = 1;
      this.loading = true;
      this.getDatas(done);
    },
    infinite (done) {
      // 上滑
      if (!this.isPull) {
        this.loading = true;
        this.getDatas(done);
      }
    },
    getDatas (done) {
      if (this.loading) {
        this.getPostList({ page: this.pageNum }).then(res => {
          this.loading = false;
          let listData = res.items || [];
          if (listData.length) {
            if (this.isPull) {
              this.isPull = false;
              this.posts = [];
            }
            this.pageNum += 1;
            const listDatas = this.posts;
            this.posts = listDatas.concat(listData);
            // this.fansList.push(...res.list);
            if (!res.isEnd) {
              done(true);
            } else {
              done(2);
            }
          } else {
            done(2);
          }
        });
      }
    }
  }
};
</script>
<style lang="less" scoped>
.forum-container {
  background: #f7f7f7;
  height: 100%;
  .body-container {
    // margin-top: 0.92rem;
    position: fixed;
    top: 0.92rem;
    width: 100%;
    bottom: 0;
    .post-list {
      .post {
        padding: 0.3rem 0.2rem;
        border-bottom: 1px solid #d2d2d2;
        display: flex;
        background: white;
        .left {
          width: 4.4rem;
          // display: inline-block;
          margin-right: 0.3rem;
        }
        .right {
          // display: inline-block;
          img {
            width: 2.3rem;
            height: 1.48rem;
          }
        }
        .title {
          font-size: 0.34rem;
          color: #333333;
          line-height: 0.48rem;
        }
        .figures {
          .figure {
            display: inline-block;
            width: 2.3rem;
            height: 1.4rem;
            &:nth-child(3n + 1) {
              margin-right: 0.1rem;
            }
            &:nth-child(3n + 2) {
              margin-right: 0.1rem;
            }
          }
        }
        .user-info {
          font-size: 0.22rem;
          color: #999999;
          margin-top: 0.2rem;
          .avatar {
            display: inline-block;
            width: 0.4rem;
            height: 0.4rem;
            margin-right: 0.1rem;
            border-radius: 50%;
          }
          .name {
            display: inline-block;
            margin-right: 20px;
          }
          .view {
            display: inline-block;
            margin-right: 20px;
          }
          .time {
            display: inline-block;
          }
        }
      }
    }
    .no-datas {
      position: fixed;
      width: 100%;
      top: 0.92rem;
      bottom: 0;
      height: 100%;
      text-align: center;
      background: #ffffff;
      img {
        display: block;
        width: 4.15rem;
        height: 3.22rem;
        margin: 0 auto;
        padding-top: 0.8rem;
      }
      .desc {
        margin-top: 0.3rem;
        font-size: 0.22rem;
        color: #919191;
      }
    }
  }
}
</style>
